﻿
Imports System.Data.OleDb
Public Class AddShowreel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            tb_User.Text = User.Identity.Name
        End If
        Dim existoleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
        Dim existsqlString As String = "SELECT aspnet_Users.[UserId], Artists.[ASPUserName] FROM aspnet_Users INNER JOIN Artists ON aspnet_Users.[UserId] = Artists.[ASPUserName] WHERE aspnet_Users.[UserName] = @f1"
        Dim existcmd As OleDbCommand = New OleDbCommand(existsqlString, existoleDbConn)
        existcmd.CommandType = CommandType.Text
        existcmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        existoleDbConn.Open()


        Dim existdataReader = existcmd.ExecuteReader()

        If (existdataReader.HasRows) = False Then
            Response.Redirect("NoArtistError.aspx")
        End If


    End Sub

 

    Protected Sub btn_AddVideo_Click(sender As Object, e As EventArgs) Handles btn_AddVideo.Click
        Dim myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() & ".mp4"
        Dim fileLocationOnServerHardDisk = Request.MapPath("videos") & "/" & newFileName
        If f_Video.HasFile Then
            'Dim fileSize As Long = FileLen(f_Video.FileName)

            Dim fileSize As Long = f_Video.PostedFile.ContentLength()

            If fileSize > 20972520 Then
                Response.Redirect("FileError.aspx")
            End If

            f_Video.SaveAs(fileLocationOnServerHardDisk)


            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)

            Dim findIdsqlString As String = "SELECT  Artists.[ID] FROM aspnet_Users  INNER JOIN Artists ON aspnet_Users.[UserId]= Artists.[ASPUserName] WHERE aspnet_Users.UserName = @f20"
            Dim findcmd As OleDbCommand = New OleDbCommand(findIdsqlString, oleDbConn)
            findcmd.CommandType = CommandType.Text
            findcmd.Parameters.AddWithValue("@f20", User.Identity.Name)
            oleDbConn.Open()
            Dim dataReader As OleDbDataReader = findcmd.ExecuteReader()
            Dim storeid As Integer

            dataReader.Read()
            storeid = dataReader.Item("ID")
            Dim storeida As String = storeid.ToString

            Dim sqlString As String = "INSERT into Video (Showreel, ArtistId, Title, Description, OriginalFilename) VALUES (@f1, @f2, @f3, @f4, @f5)"
            'Dim sqlString As String = "INSERT into Video (ArtistId, Title, Description) VALUES (@f2, @f3, @f4)"
            Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "videos/" & newFileName)
            cmd.Parameters.AddWithValue("@f2", storeida)

            cmd.Parameters.AddWithValue("@f3", tb_Title.Text)
            cmd.Parameters.AddWithValue("@f4", tb_Desc.Text)
            cmd.Parameters.AddWithValue("@f5", f_Video.FileName)




            cmd.ExecuteNonQuery()
            oleDbConn.Close()
            oleDbConn.Dispose()

            Response.Redirect("~/Artists.aspx")
        End If
    End Sub
End Class